package com.yj.web.service.impl;

import com.yj.web.dao.mapper.EmployeeMapper;
import com.yj.web.dao.mapper.IntroducerMapper;
import com.yj.web.dao.po.admin.Employee;
import com.yj.web.dao.po.admin.Introducer;
import com.yj.web.dao.po.admin.UserProjectByIntroducer;
import com.yj.web.help.PoUtil;
import com.yj.web.service.IntroducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanlinyang on 2017/7/4.
 */
@Service("introducerService")
public class IntroducerServiceImpl implements IntroducerService {

    @Autowired
    private IntroducerMapper introducerMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Introducer> listIntroducer(long employeeId, int page, int size) {
        int offset = (page - 1) * size;
        Employee employee = employeeMapper.findEmployeeById(employeeId);
        List<Introducer> introducers = new ArrayList<>();
        switch (employee.getRoleNumber()) {
            case 1:
                introducers = introducerMapper.listIntroducerByAdmin(offset, size);
                break;
            case 3:
//                List<Employee> employees = employeeMapper.listEmployeeByManager(employeeId);
                List<Long> employeeIds = employeeMapper.listEmployeeIdByManager(employeeId);
                employeeIds.add(employee.getId());
                introducers = introducerMapper.listIntroducerByManager(employeeIds, offset, size);
                break;
            case 4:
                introducers = introducerMapper.listIntroducerByEmployee(employeeId, offset, size);
                break;
            default:
                break;
        }
        if (null != introducers && introducers.size() > 0) {
            for (Introducer introducer : introducers) {
                String employeeName = employeeMapper.getNameById(introducer.getLink_employee());
                introducer.setEmployeeName(employeeName);
            }
        }
        return introducers;
    }

    @Override
    public int countIntroducer(long employeeId) {
        Employee employee = employeeMapper.findEmployeeById(employeeId);
        int num = 0;//
        switch (employee.getRoleNumber()) {
            case 1:
                num = introducerMapper.countIntroducerByAdmin();
                break;
            case 3:
//                List<Employee> employees = employeeMapper.listEmployeeByManager(employeeId);
                List<Long> employeeIds = employeeMapper.listEmployeeIdByManager(employeeId);
                employeeIds.add(employee.getId());
                num = introducerMapper.countIntroducerByManager(employeeIds);
                break;
            case 4:
                num = introducerMapper.countIntroducerByEmployee(employeeId);
                break;
            default:
                break;
        }
        return num;
    }

    @Override
    public int deleteIntroducerById(long id) {
        int num = introducerMapper.deleteIntroducerById(id);
        return num;
    }

    @Override
    public long insertIntroducer(Introducer introducer) {
        return introducerMapper.insertIntroducer(introducer);
    }

    @Override
    public boolean updateIntroducer(Introducer introducer) {
        Introducer introducerOriginal = introducerMapper.findIntroducerById(introducer.getId());
        if(introducerOriginal == null) {
            return false;
        }
        try {
            PoUtil.supplementPo(introducer, introducerOriginal);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return introducerMapper.updateIntroducer(introducerOriginal);
    }

    @Override
    public boolean updateIntroducerStatus(int id, int status) {
        return introducerMapper.updateIntroducerStatus(id, status);
    }

    @Override
    public List<UserProjectByIntroducer> listUserProjectByIntroducer(String nick_name, String introducer_name, String phone, long employeeId, int page, int size) {
        int offset = (page - 1) * size;
        Employee employee = employeeMapper.findEmployeeById(employeeId);
        List<Employee> employees = new ArrayList<>();
        List<UserProjectByIntroducer> userProjectByIntroducers = new ArrayList<>();
        switch (employee.getRoleNumber()) {
            case 1:
                employees = employeeMapper.listAllEmployeeByManager();
                break;
            case 3:
                employees = employeeMapper.listEmployeeByManager(employeeId);
                employees.add(employee);
                break;
            case 4:
                employees.add(employee);
                break;
            default:
                break;
        }
        if (0 == employees.size()) {
            employees = null;
        }
        userProjectByIntroducers = introducerMapper.listUserProjectByIntroducer(nick_name, introducer_name, phone, employees, offset, size);
        return userProjectByIntroducers;
    }

    public int countUserProjectByIntroducer(String nick_name, String introducer_name, String phone, long employeeId) {
        Employee employee = employeeMapper.findEmployeeById(employeeId);
        List<Employee> employees = new ArrayList<>();
        int num = 0;
        switch (employee.getRoleNumber()) {
            case 1:
                employees = employeeMapper.listAllEmployeeByManager();
                break;
            case 3:
                employees = employeeMapper.listEmployeeByManager(employeeId);
                employees.add(employee);
                break;
            case 4:
                employees.add(employee);
                break;
            default:
                break;
        }
        if (0 == employees.size()) {
            employees = null;
        }
        num = introducerMapper.countUserProjectByIntroducer(nick_name, introducer_name, phone, employees);
        return num;
    }

    public Introducer getIntroducerById(long id) {
        Introducer introducer = introducerMapper.findIntroducerById(id);
        return introducer;
    }

    @Override
    public List<Employee> introducerRelatedEmployee(long employeeId) {
        Employee employee = employeeMapper.findEmployeeById(employeeId);
        List<Employee> employees = new ArrayList<>();
        switch (employee.getRoleNumber()) {
            case 1:
                employees = employeeMapper.listAllEmployeeByManager();
                break;
            case 3:
                employees = employeeMapper.listEmployeeByManager(employeeId);
                employees.add(employee);
                break;
            case 4:
                employees.add(employee);
                break;
            default:
                break;
        }
        return employees;
    }

    @Override
    public boolean updateQrcode(int id, String QRcode) {
        return introducerMapper.updateQrcode(id, QRcode);
    }

    @Override
    public String getIntroPhoneByOpenId(String openId) {
        return introducerMapper.getIntroducerPhoneByOpenId(openId);
    }
}
